package com.zhiyu.controller;

import com.github.pagehelper.PageInfo;
import com.zhiyu.common.ApiReturn;
import com.zhiyu.util.QiniuCloudUtil;
import com.zhiyu.pojo.Products;
import com.zhiyu.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

@RequestMapping("product")
@Controller
@CrossOrigin
public class ProductController {

    @Autowired
    private ProductsService productsService;

    //商品管理
    @RequestMapping("productsManage")
    @ResponseBody
    public ApiReturn productsManage(Products products){
        PageInfo<Products> page = productsService.findPage(products);
        if (page.getList()!=null){
            return ApiReturn.success("返回商品数据",page);
        }
        return ApiReturn.error("无数据");
    }

    //删除商品
    @RequestMapping("deleteProduct")
    @ResponseBody
    public ApiReturn deleteProduct(Products products){
        int count = productsService.delete(products);
        if (count<1){
            return ApiReturn.error("删除失败");
        }
        return ApiReturn.success("删除成功");
    }

    //查询商品
    @RequestMapping("selectProduct")
    @ResponseBody
    public ApiReturn selectProduct(Products products){
        Products productsInfo = productsService.get(products.getProId());
        if (productsInfo != null){
            return ApiReturn.success("商品数据详情",productsInfo);
        }
        return ApiReturn.error("未获取该商品数据详情");
    }

    //修改商品
    @RequestMapping("updateProduct")
    @ResponseBody
    public ApiReturn updateProduct(Products products){
        int count = productsService.update(products);
        if (count<1){
            return ApiReturn.error("修改失败");
        }
        return ApiReturn.success("修改成功");
    }

    //添加商品
    @RequestMapping("addProduct")
    @ResponseBody
    public ApiReturn addProduct(Products products){
        products.setGmtCreate(new Date());
        int count = productsService.insert(products);
        if (count<1){
            return ApiReturn.error("添加失败");
        }
        return ApiReturn.success("添加成功");
    }

   /* //本地上传图片
    @RequestMapping("uploadPic")
    @ResponseBody
    public ApiReturn uploadPic(@RequestParam("file") MultipartFile file) throws IOException {
        String path = null;
        if (file!=null){
            String fileName = file.getOriginalFilename();
            String type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type!=null){
                if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + "." + type;
                    file.transferTo(new File(path+trueFileName));
                    return ApiReturn.success("上传成功");
                }
            }
        }
        return ApiReturn.error("上传失败");
    }*/

   //七牛云图片上传
   @RequestMapping("uploadPic")
   @ResponseBody
   public ApiReturn uploadPic(@RequestParam("file") MultipartFile file) throws IOException {
       if (file!=null){
           File file1 = new File("d:\\test");
           file.transferTo(file1);
           String imgName = String.valueOf(System.currentTimeMillis());
           String url = QiniuCloudUtil.upload(file1,imgName);
           file1.delete();
           return ApiReturn.success("上传成功",imgName);
       }
       return ApiReturn.error("上传失败");
   }

   //根据花材种类分页获得商品
   @GetMapping("getProductsByFlowerWood")
   @ResponseBody
   public ApiReturn getProductsByFlowerWood(Products products){
       PageInfo<Products> page = productsService.getProByFlowerWood(products);
       if (page.getList()!=null){
           return ApiReturn.success("返回商品数据",page);
       }
       return ApiReturn.error("无数据");
   }

   //根据商品名称搜索
   @GetMapping("getProductsByName")
   @ResponseBody
   public ApiReturn getProductByName(Products products){
       PageInfo<Products> page = productsService.getProductByName(products);
       if (page.getList()!=null){
           return ApiReturn.success("返回商品数据",page);
       }
       return ApiReturn.error("无数据");
   }
}
